package com.bigshen.mvc.demo;

import com.bigshen.mvc.demo.service.IDemoService;
import com.bigshen.mvc.spring.annonation.ShenAutowired;
import com.bigshen.mvc.spring.annonation.ShenController;
import com.bigshen.mvc.spring.annonation.ShenRequestMapping;
import com.bigshen.mvc.spring.annonation.ShenRequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author eddy.shen
 * @Date 2020/4/5 15:37
 **/
@ShenController
@ShenRequestMapping("/demo")
public class DemoController {

    @ShenAutowired
    private IDemoService demoService;

    @ShenRequestMapping("/sum")
    public void sum(HttpServletRequest req, HttpServletResponse resp,
                    @ShenRequestParam("a")Integer a, @ShenRequestParam("b") Integer b) {

        Integer sum = demoService.add(a, b);
        try {
            resp.getWriter().write(String.format("a[%s] + b[%s] = %s", a, b, sum));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ShenRequestMapping("/getName")
    public void getName(HttpServletRequest req, HttpServletResponse resp,
                    @ShenRequestParam("name")String name) {

        String result = demoService.getName(name);
        try {
            resp.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
